import { SAVE_USERINFO,DELETE_USERINFO } from "../constant"

// 【自动登录】从localStorage取出，放入初始化状态，这样刷新admin页面不会清空redux，不会打回login页面
let user = JSON.parse(localStorage.getItem("user"))  //因为user状态为对象格式，所以要解析为对象
let token = localStorage.getItem("token")
const initState = {
    //【注意】：此处要进行判断：如果有user用user，没有就空
    user: user||"",
    token: token || "",
    isLogin: user && token ? true:false
}
let login_reducer = (preState = initState, action) => {
    const { type, data } = action
    switch (type) {
        case SAVE_USERINFO:
            return {user:data.user,token:data.token,isLogin:true}
        case DELETE_USERINFO:
            return {user:"",token:"",isLogin:false}
        default:
            return  preState
    }
}
export default login_reducer
